import win32gui
from SetWindow import SetWindow
from duihua import *
import setting
import yaml
import TranslateAPI
from SelectText import get_selected_text

def loadpic():
    pic=[]
    pic.append(pygame.image.load("cat1.png").convert_alpha())
    pic[0]=pygame.transform.scale(pic[0],(300,300))
    pic.append(pygame.image.load("cat2.png").convert_alpha())
    pic[1] = pygame.transform.scale(pic[1], (300, 300))
    pic.append(pygame.image.load("cat3.png").convert_alpha())
    pic[2] = pygame.transform.scale(pic[2], (300, 300))
    return pic

def win1():
    pygame.init()
    screen = pygame.display.set_mode((550, 400), pygame.NOFRAME | pygame.SRCALPHA)
    picture=loadpic()

    pygame.display.set_caption("DesktopPet")
    hwnd=win32gui.FindWindow(None,"DesktopPet")
    icon = pygame.image.load("icon.ico")
    pygame.display.set_icon(icon)


    font_name = pygame.font.match_font('fangsong')
    font = pygame.font.Font(font_name, 20)
    response=None
    dragger = SetWindow(hwnd)
    clock = pygame.time.Clock()
    running = True
    window3=None
    downtime=0
    lasttime=0
    clock_cnt=0
    with open('config.yaml', 'r') as file:
        config = yaml.load(file, Loader=yaml.FullLoader)
    SingleClick=300
    DoubleClicks=300
    while running:
        screen.fill((0,0,0))
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    dragger.start_drag()
                    downtime=pygame.time.get_ticks()

            elif event.type == pygame.MOUSEBUTTONUP:
                if event.button == 1:
                    dragger.dragging = False
                    uptime=pygame.time.get_ticks()
                    if uptime-downtime<SingleClick:
                        #单击
                        if uptime-lasttime<DoubleClicks:
                            #双击
                            window3= setting.SettingWindow()
                    lasttime=uptime
                elif event.button == 3:
                    # 右键单击
                    if config['input'] == 0:
                        # 长文本输入
                        window=InputWindow()
                        user_input = window.get_input()
                    else:
                        # 剪贴板输入
                        user_input=get_selected_text()


                    if user_input != '':
                        # 获取输出
                        if config['mode'] == 0:
                            # ai对话
                            response = get_response(user_input)

                        else:
                            # 百度翻译
                            tlwindow = TranslateAPI.BAIDUTranslation()
                            response = tlwindow.translation(user_input)

                        # 输出回答
                        if config['output'] == 0:
                            # 气泡回答
                            text_split(response)
                            #print(response)
                        elif config['output'] == 1:
                            # 长文本回答
                            window2=Output(response)
                            window2.show()

            elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                running = False

        if response is not None and config['output']==0:
            output_bubble(font,screen)

        # 实时更新拖动
        dragger.update_drag()
        # 处理Windows消息
        win32gui.PumpWaitingMessages()

        #改变形象
        if win32gui.GetForegroundWindow()==hwnd:
            if clock_cnt <5 :
                pic=picture[1]
            else:
                pic=picture[0]
        else:
            pic=picture[2]

        screen.blit(pic,(250,0))

        pygame.display.flip()

        if setting.saved==1:
            with open('config.yaml', 'r') as file:
                config = yaml.load(file, Loader=yaml.FullLoader)
            setting.saved=0

        clock_cnt=(clock_cnt+1)%150
        clock.tick(60)

    pygame.quit()

if __name__ == "__main__":
    win1()